package com.yeyks.common.appcommon.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * 通用返回对象
 *
 * @author Administrator
 */
@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class CommonResult<T> {
    /**
     * 成功
     */
    public static final int SUCCESS = 200;
    /**
     * 酒店抢单码
     */
    public static final int GRAB_SUCCESS = 200001;
    /**
     * 抢单成功
     */
    public static final int HOTEL_GRAB_SUCCESS = 200002;
    /**
     * 抢单失败
     */
    public static final int HOTEL_GRAB_FAIL = 200003;
    /**
     * 常用失败
     */
    public static final int FAILED = 400;
    /**
     * token失效
     */
    public static final int TOKEN_ENABLE = 401;
    /**
     * 没有权限
     */
    public static final int NO_PERMISSION = 403;

    /**
     * 权限更新
     */
    public static final int UPDATE_PERMISSION = 403001;
    /**
     * 不好意思, 服务开小差了, 即刻为你修复
     */
    public static final int ERROR = 500;
    /**
     * 数据库中已存在该记录
     */
    public static final int DATA_IS_EXIST = 405;

    public static final int VALIDATE_FAILED = 2;
    private int code;
    private String msg;
    private T data;

    /**
     * 普通成功返回
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 操作状态成功等信息, 需要返回消息
     *
     * @param msg  成功信息
     * @param data 数据
     * @return 返回结果
     */
    public static <T> CommonResult<T> success(T data, String msg) {
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(SUCCESS);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    /**
     * 普通失败提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        CommonResult result = new CommonResult();
        result.setCode(FAILED);
        result.setMsg(message);
        return result;
    }

    /**
     * 普通失败提示信息
     */
    public static <T> CommonResult<T> tokenEnable(String message) {
        CommonResult result = new CommonResult();
        result.setCode(TOKEN_ENABLE);
        result.setMsg(message);
        return result;
    }

    /**
     * 参数验证失败使用
     *
     * @param message 错误信息
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        CommonResult result = new CommonResult();
        result.setCode(VALIDATE_FAILED);
        result.setMsg(message);
        return result;
    }

    /**
     * 抛出异常时使用
     *
     * @return 返回信息
     */
    public static <T> CommonResult<T> create(T data, int code, String msg) {
        CommonResult<T> result = new CommonResult<T>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
